from operator import mod
from statistics import mode
from unicodedata import category
from django.db import models

# Create your models here.
class GeoInfo(models.Model):
    class Status(models.IntegerChoices):
        NORMAL = 0, '正常'
        DOUBT = 1, '不确定'
        OUT = 2, '停用'
        DELETE = 3,  '已删除'

    name = models.CharField(max_length=100, verbose_name="点位")
    longitude = models.DecimalField(max_digits=10, decimal_places=6, verbose_name="经度")
    latitude = models.DecimalField(max_digits=10, decimal_places=6, verbose_name="纬度")
    elevation = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="海拔")
    address = models.CharField(max_length=200, verbose_name="地址")
    tips = models.TextField(blank=True, null=True, verbose_name="备注")
    createtime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updatetime = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    status = models.IntegerField(choices=Status.choices, default=Status.NORMAL, verbose_name="状态")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = verbose_name_plural = "点位地理信息"

class Mileage(models.Model):
    class Status(models.IntegerChoices):
        NORMAL = 0, '正常'
        DOUBT = 1, '不确定'
        OUT = 2, '停用'
        DELETE = 3,  '已删除'

    name = models.CharField(max_length=40, verbose_name="区间")
    startpoint = models.ForeignKey(GeoInfo, on_delete=models.PROTECT, related_name="Mileage_Start", verbose_name="起点")
    destination = models.ForeignKey(GeoInfo, on_delete=models.PROTECT, related_name="Mileage_End", verbose_name="终点")
    mileage = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="里程")
    tips = models.TextField(blank=True, null=True, verbose_name="备注")
    createtime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updatetime = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    status = models.IntegerField(choices=Status.choices, default=Status.NORMAL, verbose_name="状态")

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = verbose_name_plural = "路线信息"

class CateInfo(models.Model):
    class Status(models.IntegerChoices):
        ONE = 0, '一级类别'
        TWO = 1, '二级类别'
        THR = 2, '三级类别'

    name = models.CharField(max_length=100, verbose_name="名称")    
    # category = models.CharField(max_length=40, verbose_name="类别")
    pid = models.PositiveIntegerField(verbose_name="类别id")
    grade = models.IntegerField(choices=Status.choices, default=Status.ONE, verbose_name="级别")

    def __str__(self):
        return str(self.name)

    class Meta:
        verbose_name = verbose_name_plural = "类别信息"

class DataInfo(models.Model):
    class Status(models.IntegerChoices):
        NORMAL = 0, '正常'
        DOUBT = 1, '不确定'
        OUT = 2, '停用'
        DELETE = 3,  '已删除'

    place = models.ForeignKey(GeoInfo, on_delete=models.PROTECT, related_name="DataInfo_place", verbose_name="点位")
    name = models.ForeignKey(CateInfo, on_delete=models.PROTECT, verbose_name="名称")
    data = models.DecimalField(max_digits=12, decimal_places=2, verbose_name="数值")
    unit = models.CharField(max_length=20, verbose_name="计量单位")
    tips = models.TextField(blank=True, null=True, verbose_name="备注")
    createtime = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    updatetime = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    status = models.IntegerField(choices=Status.choices, default=Status.NORMAL, verbose_name="状态")

    def __str__(self):
        return str(self.name)

    class Meta:
        verbose_name = verbose_name_plural = "点位数据"